/**
 * HomeController
 *
 * @description :: Server-side logic for managing homes
 * @help        :: See http://sailsjs.org/#!/documentation/concepts/Controllers
 */
var moment = require('moment');

module.exports = {
  index: function (req, res) {
    var Promise = require('promise');
    Token.find({type: 'access_token'}).then(function (result) {
      var now = moment().valueOf();
      if (now - result[0].createTime < result[0].expire) {
        //console.log(now - result.createTime);
        return result[0].value;
      } else {
        return result[0].type;
      }
    }).then(function (token) {
      console.log(token);
      req.session.accessToken = token;
      res.view('index');
    }).catch(function (err) {
      res.serverError(err);
    });

  },

  login: function (req, res) {
    if (req.params.name == 'admin') {
      req.session.openId = 'admin123456';
      res.redirect('home');
    }
  },
}
;

